﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore.Extensions
{
    public static class RSAExtensions
    {
        /// <summary>
        /// 公钥加密
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public static string ToEncryptRSAString(this string plainText)
        {
            using (var rsa = RSA.Create())
            {
                var publicKey = File.ReadAllBytes("public_key.pem");
                rsa.ImportSubjectPublicKeyInfo(publicKey, out _);
                var bytesToEncrypt = Encoding.UTF8.GetBytes(plainText);
                var encryptedBytes = rsa.Encrypt(bytesToEncrypt, RSAEncryptionPadding.OaepSHA256);
                return Convert.ToBase64String(encryptedBytes);
            }
        }

        /// <summary>
        /// 私钥解密
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public static string ToDecryptRSAString(this string plainText)
        {
            using (var rsa = RSA.Create())
            {
                var privateKey = File.ReadAllBytes("private_key.pem");
                rsa.ImportPkcs8PrivateKey(privateKey, out _);
                var bytesToDecrypt = Convert.FromBase64String(plainText);
                var decryptedBytes = rsa.Decrypt(bytesToDecrypt, RSAEncryptionPadding.OaepSHA256);
                return Encoding.UTF8.GetString(decryptedBytes);
            }
        }

        /// <summary>
        /// 生成签名
        /// </summary>
        /// <param name="content"></param>
        /// <param name="decryptKey"></param>
        /// <returns></returns>
        public static string SignData(this string plainText)
        {
            using (var rsa = RSA.Create())
            {
                var privateKey = File.ReadAllBytes("private_key.pem");
                byte[] bytes = Encoding.UTF8.GetBytes(plainText);
                rsa.ImportPkcs8PrivateKey(privateKey, out _);
                byte[] sign = rsa.SignData(bytes, 0, bytes.Length, HashAlgorithmName.SHA256, RSASignaturePadding.Pss);
                return Convert.ToBase64String(sign);
            }
        }

        /// <summary>
        /// 验证签名
        /// </summary>
        /// <param name="content"></param>
        /// <param name="decryptKey"></param>
        /// <returns></returns>
        public static bool VerifyData(this string plainText, string signatureBase64)
        {
            using (var rsa = RSA.Create())
            {
                var publicKey = File.ReadAllBytes("public_key.pem");
                rsa.ImportSubjectPublicKeyInfo(publicKey, out _);
                byte[] bytes = Encoding.UTF8.GetBytes(plainText);
                var verify = rsa.VerifyData(bytes, Convert.FromBase64String(signatureBase64), HashAlgorithmName.SHA256, RSASignaturePadding.Pss);
                return verify;
            }
        }

        /// <summary>
        /// 生成密钥对
        /// </summary>
        public static void GenerateKeyPair()
        {
            using (RSA rsa = RSA.Create())
            {
                // 导出私钥
                var privateKeyBytes = rsa.ExportPkcs8PrivateKey();
                File.WriteAllBytes("private_key.pem", privateKeyBytes);

                // 导出公钥
                var publicKeyBytes = rsa.ExportSubjectPublicKeyInfo();
                File.WriteAllBytes("public_key.pem", publicKeyBytes);
            }
        }
    }
}
